package com.xxmw.transaction.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xxmw.transaction.common.annotation.LoginUser;
import com.xxmw.transaction.common.annotation.UserLoginIgnore;
import com.xxmw.transaction.common.entity.R;
import com.xxmw.transaction.common.utils.Assert;
import com.xxmw.transaction.model.app.User;
import com.xxmw.transaction.model.app.VipPrice;
import com.xxmw.transaction.model.app.VipRecord;
import com.xxmw.transaction.model.app.vo.BitWorldResponse;
import com.xxmw.transaction.service.app.IVipPriceService;
import com.xxmw.transaction.service.app.IVipRecordService;
import com.xxmw.transaction.service.app.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;


/**
 * Vip价格 controller
 */
@Slf4j
@RestController
@RequestMapping("/app/vipPrice")
@Api(tags = "vip相关接口")
public class VipPriceController {

    @Autowired
    private IVipPriceService vipPriceService;

    @Autowired
    private IVipRecordService recordService;

    @Autowired
    private UserService userService;

    @GetMapping("list")
    @UserLoginIgnore
    @ApiOperation("获取价格列表")
    public R list() {
        LambdaQueryWrapper<VipPrice> wrapper = new LambdaQueryWrapper<VipPrice>();
        wrapper.eq(VipPrice::getStatus, VipPrice.Status.PUBLISHED.value()).orderByAsc(VipPrice::getMonthCount);
        List<VipPrice> list = vipPriceService.list(wrapper);
        return R.ok(list);
    }

     /**
     * 开通VIP
     * @param username 用户名
     * @param typeId 开通类型
     * @return 支付URL
     */
    @PostMapping("getVip")
    @ApiOperation("开通vip")
    public R getVip (@ApiIgnore @LoginUser("username") String username,
                     @RequestParam Integer typeId) {
        VipPrice price = vipPriceService.getById(typeId);
        HashMap<String, Object> result = recordService.createOrder(username, price);
        return R.ok(result);
    }

    /**
     * 领取7天vip福利
     * @param username
     * @return
     */
    @PostMapping("testVip")
    @ApiOperation("领取VIP福利")
    public R getVip (@ApiIgnore @LoginUser("username") String username) {
        // 增加VIP时间
        userService.testVip(username);
        log.info("用户{}续费VIP,VIP续费时长：{}天", username, 7);
        return R.ok();
    }

    @GetMapping("orderInfo")
    @ApiOperation("查询订单状态")
    public R getOrderInfo (@RequestParam String id) {
        VipRecord record = recordService.getById(id);
        if (Assert.isNullOrEmpty(record)) {
            return R.error("订单不存在");
        }
        if (VipRecord.Status.FINISHED.getValue().equals(record.getStatus())) {
            return R.ok(true);
        } else {
            return R.ok(false);
        }

    }

    @PostMapping("notice")
    @UserLoginIgnore
    @ApiIgnore
    public String notice (@RequestBody BitWorldResponse response) {
        log.info("收到回调：{}", response.toString());
        // 验证返回数据的有效性，防止恶意开通
        if (recordService.verifySign(response.getSignInfo(), response.getOrderNo(), response.getRandomStr())) {
            log.info("验证bitWorld签名成功！");
            String orderId = response.getOrderNo();
            recordService.orderSuccess(orderId);
            return "true";
        }
        log.info("验证bitWorld签名失败！");
        return "false";
    };

    @ApiIgnore
    @GetMapping("test")
    public R test(@LoginUser("username") String username,
                  @LoginUser("id") Long userId,
                  @LoginUser User user) {
        log.info(username);
        log.info(String.valueOf(userId));
        log.info(String.valueOf(user));
        return  R.ok();
    }

}
